| Conditions | 1 |
| Paths | 128 |
| Total Lines | 1450 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 812 | plupload.Uploader = function(options) { |
||
| 813 | /** |
||
| 814 | Fires when the current RunTime has been initialized. |
||
| 815 | |||
| 816 | @event Init |
||
| 817 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 818 | */ |
||
| 819 | |||
| 820 | /** |
||
| 821 | Fires after the init event incase you need to perform actions there. |
||
| 822 | |||
| 823 | @event PostInit |
||
| 824 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 825 | */ |
||
| 826 | |||
| 827 | /** |
||
| 828 | Fires when the option is changed in via uploader.setOption(). |
||
| 829 | |||
| 830 | @event OptionChanged |
||
| 831 | @since 2.1 |
||
| 832 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 833 | @param {String} name Name of the option that was changed |
||
| 834 | @param {Mixed} value New value for the specified option |
||
| 835 | @param {Mixed} oldValue Previous value of the option |
||
| 836 | */ |
||
| 837 | |||
| 838 | /** |
||
| 839 | Fires when the silverlight/flash or other shim needs to move. |
||
| 840 | |||
| 841 | @event Refresh |
||
| 842 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 843 | */ |
||
| 844 | |||
| 845 | /** |
||
| 846 | Fires when the overall state is being changed for the upload queue. |
||
| 847 | |||
| 848 | @event StateChanged |
||
| 849 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 850 | */ |
||
| 851 | |||
| 852 | /** |
||
| 853 | Fires when browse_button is clicked and browse dialog shows. |
||
| 854 | |||
| 855 | @event Browse |
||
| 856 | @since 2.1.2 |
||
| 857 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 858 | */ |
||
| 859 | |||
| 860 | /** |
||
| 861 | Fires for every filtered file before it is added to the queue. |
||
| 862 | |||
| 863 | @event FileFiltered |
||
| 864 | @since 2.1 |
||
| 865 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 866 | @param {plupload.File} file Another file that has to be added to the queue. |
||
| 867 | */ |
||
| 868 | |||
| 869 | /** |
||
| 870 | Fires when the file queue is changed. In other words when files are added/removed to the files array of the uploader instance. |
||
| 871 | |||
| 872 | @event QueueChanged |
||
| 873 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 874 | */ |
||
| 875 | |||
| 876 | /** |
||
| 877 | Fires after files were filtered and added to the queue. |
||
| 878 | |||
| 879 | @event FilesAdded |
||
| 880 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 881 | @param {Array} files Array of file objects that were added to queue by the user. |
||
| 882 | */ |
||
| 883 | |||
| 884 | /** |
||
| 885 | Fires when file is removed from the queue. |
||
| 886 | |||
| 887 | @event FilesRemoved |
||
| 888 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 889 | @param {Array} files Array of files that got removed. |
||
| 890 | */ |
||
| 891 | |||
| 892 | /** |
||
| 893 | Fires just before a file is uploaded. Can be used to cancel the upload for the specified file |
||
| 894 | by returning false from the handler. |
||
| 895 | |||
| 896 | @event BeforeUpload |
||
| 897 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 898 | @param {plupload.File} file File to be uploaded. |
||
| 899 | */ |
||
| 900 | |||
| 901 | /** |
||
| 902 | Fires when a file is to be uploaded by the runtime. |
||
| 903 | |||
| 904 | @event UploadFile |
||
| 905 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 906 | @param {plupload.File} file File to be uploaded. |
||
| 907 | */ |
||
| 908 | |||
| 909 | /** |
||
| 910 | Fires while a file is being uploaded. Use this event to update the current file upload progress. |
||
| 911 | |||
| 912 | @event UploadProgress |
||
| 913 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 914 | @param {plupload.File} file File that is currently being uploaded. |
||
| 915 | */ |
||
| 916 | |||
| 917 | /** |
||
| 918 | * Fires just before a chunk is uploaded. This event enables you to override settings |
||
| 919 | * on the uploader instance before the chunk is uploaded. |
||
| 920 | * |
||
| 921 | * @event BeforeChunkUpload |
||
| 922 | * @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 923 | * @param {plupload.File} file File to be uploaded. |
||
| 924 | * @param {Object} args POST params to be sent. |
||
| 925 | * @param {Blob} chunkBlob Current blob. |
||
| 926 | * @param {offset} offset Current offset. |
||
| 927 | */ |
||
| 928 | |||
| 929 | /** |
||
| 930 | Fires when file chunk is uploaded. |
||
| 931 | |||
| 932 | @event ChunkUploaded |
||
| 933 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 934 | @param {plupload.File} file File that the chunk was uploaded for. |
||
| 935 | @param {Object} result Object with response properties. |
||
| 936 | @param {Number} result.offset The amount of bytes the server has received so far, including this chunk. |
||
| 937 | @param {Number} result.total The size of the file. |
||
| 938 | @param {String} result.response The response body sent by the server. |
||
| 939 | @param {Number} result.status The HTTP status code sent by the server. |
||
| 940 | @param {String} result.responseHeaders All the response headers as a single string. |
||
| 941 | */ |
||
| 942 | |||
| 943 | /** |
||
| 944 | Fires when a file is successfully uploaded. |
||
| 945 | |||
| 946 | @event FileUploaded |
||
| 947 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 948 | @param {plupload.File} file File that was uploaded. |
||
| 949 | @param {Object} result Object with response properties. |
||
| 950 | @param {String} result.response The response body sent by the server. |
||
| 951 | @param {Number} result.status The HTTP status code sent by the server. |
||
| 952 | @param {String} result.responseHeaders All the response headers as a single string. |
||
| 953 | */ |
||
| 954 | |||
| 955 | /** |
||
| 956 | Fires when all files in a queue are uploaded. |
||
| 957 | |||
| 958 | @event UploadComplete |
||
| 959 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 960 | @param {Array} files Array of file objects that was added to queue/selected by the user. |
||
| 961 | */ |
||
| 962 | |||
| 963 | /** |
||
| 964 | Fires when a error occurs. |
||
| 965 | |||
| 966 | @event Error |
||
| 967 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 968 | @param {Object} error Contains code, message and sometimes file and other details. |
||
| 969 | @param {Number} error.code The plupload error code. |
||
| 970 | @param {String} error.message Description of the error (uses i18n). |
||
| 971 | */ |
||
| 972 | |||
| 973 | /** |
||
| 974 | Fires when destroy method is called. |
||
| 975 | |||
| 976 | @event Destroy |
||
| 977 | @param {plupload.Uploader} uploader Uploader instance sending the event. |
||
| 978 | */ |
||
| 979 | var uid = plupload.guid() |
||
| 980 | , settings |
||
| 981 | , files = [] |
||
| 982 | , preferred_caps = {} |
||
| 983 | , fileInputs = [] |
||
| 984 | , fileDrops = [] |
||
| 985 | , startTime |
||
| 986 | , total |
||
| 987 | , disabled = false |
||
| 988 | , xhr |
||
| 989 | ; |
||
| 990 | |||
| 991 | |||
| 992 | // Private methods |
||
| 993 | function uploadNext() { |
||
| 994 | var file, count = 0, i; |
||
| 995 | |||
| 996 | if (this.state == plupload.STARTED) { |
||
| 997 | // Find first QUEUED file |
||
| 998 | for (i = 0; i < files.length; i++) { |
||
| 999 | if (!file && files[i].status == plupload.QUEUED) { |
||
| 1000 | file = files[i]; |
||
| 1001 | if (this.trigger("BeforeUpload", file)) { |
||
| 1002 | file.status = plupload.UPLOADING; |
||
| 1003 | this.trigger("UploadFile", file); |
||
| 1004 | } |
||
| 1005 | } else { |
||
| 1006 | count++; |
||
| 1007 | } |
||
| 1008 | } |
||
| 1009 | |||
| 1010 | // All files are DONE or FAILED |
||
| 1011 | if (count == files.length) { |
||
| 1012 | if (this.state !== plupload.STOPPED) { |
||
| 1013 | this.state = plupload.STOPPED; |
||
| 1014 | this.trigger("StateChanged"); |
||
| 1015 | } |
||
| 1016 | this.trigger("UploadComplete", files); |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | |||
| 1022 | function calcFile(file) { |
||
| 1023 | file.percent = file.size > 0 ? Math.ceil(file.loaded / file.size * 100) : 100; |
||
| 1024 | calc(); |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | |||
| 1028 | function calc() { |
||
| 1029 | var i, file; |
||
| 1030 | var loaded; |
||
| 1031 | var loadedDuringCurrentSession = 0; |
||
| 1032 | |||
| 1033 | // Reset stats |
||
| 1034 | total.reset(); |
||
| 1035 | |||
| 1036 | // Check status, size, loaded etc on all files |
||
| 1037 | for (i = 0; i < files.length; i++) { |
||
| 1038 | file = files[i]; |
||
| 1039 | |||
| 1040 | if (file.size !== undef) { |
||
| 1041 | // We calculate totals based on original file size |
||
| 1042 | total.size += file.origSize; |
||
| 1043 | |||
| 1044 | // Since we cannot predict file size after resize, we do opposite and |
||
| 1045 | // interpolate loaded amount to match magnitude of total |
||
| 1046 | loaded = file.loaded * file.origSize / file.size; |
||
| 1047 | |||
| 1048 | if (!file.completeTimestamp || file.completeTimestamp > startTime) { |
||
| 1049 | loadedDuringCurrentSession += loaded; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | total.loaded += loaded; |
||
| 1053 | } else { |
||
| 1054 | total.size = undef; |
||
| 1055 | } |
||
| 1056 | |||
| 1057 | if (file.status == plupload.DONE) { |
||
| 1058 | total.uploaded++; |
||
| 1059 | } else if (file.status == plupload.FAILED) { |
||
| 1060 | total.failed++; |
||
| 1061 | } else { |
||
| 1062 | total.queued++; |
||
| 1063 | } |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | // If we couldn't calculate a total file size then use the number of files to calc percent |
||
| 1067 | if (total.size === undef) { |
||
| 1068 | total.percent = files.length > 0 ? Math.ceil(total.uploaded / files.length * 100) : 0; |
||
| 1069 | } else { |
||
| 1070 | total.bytesPerSec = Math.ceil(loadedDuringCurrentSession / ((+new Date() - startTime || 1) / 1000.0)); |
||
| 1071 | total.percent = total.size > 0 ? Math.ceil(total.loaded / total.size * 100) : 0; |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 | |||
| 1075 | |||
| 1076 | function getRUID() { |
||
| 1077 | var ctrl = fileInputs[0] || fileDrops[0]; |
||
| 1078 | if (ctrl) { |
||
| 1079 | return ctrl.getRuntime().uid; |
||
| 1080 | } |
||
| 1081 | return false; |
||
| 1082 | } |
||
| 1083 | |||
| 1084 | |||
| 1085 | function bindEventListeners() { |
||
| 1086 | this.bind('FilesAdded FilesRemoved', function(up) { |
||
| 1087 | up.trigger('QueueChanged'); |
||
| 1088 | up.refresh(); |
||
| 1089 | }); |
||
| 1090 | |||
| 1091 | this.bind('CancelUpload', onCancelUpload); |
||
| 1092 | |||
| 1093 | this.bind('BeforeUpload', onBeforeUpload); |
||
| 1094 | |||
| 1095 | this.bind('UploadFile', onUploadFile); |
||
| 1096 | |||
| 1097 | this.bind('UploadProgress', onUploadProgress); |
||
| 1098 | |||
| 1099 | this.bind('StateChanged', onStateChanged); |
||
| 1100 | |||
| 1101 | this.bind('QueueChanged', calc); |
||
| 1102 | |||
| 1103 | this.bind('Error', onError); |
||
| 1104 | |||
| 1105 | this.bind('FileUploaded', onFileUploaded); |
||
| 1106 | |||
| 1107 | this.bind('Destroy', onDestroy); |
||
| 1108 | } |
||
| 1109 | |||
| 1110 | |||
| 1111 | function initControls(settings, cb) { |
||
| 1112 | var self = this, inited = 0, queue = []; |
||
| 1113 | |||
| 1114 | // common settings |
||
| 1115 | var options = { |
||
| 1116 | runtime_order: settings.runtimes, |
||
| 1117 | required_caps: settings.required_features, |
||
| 1118 | preferred_caps: preferred_caps, |
||
| 1119 | swf_url: settings.flash_swf_url, |
||
| 1120 | xap_url: settings.silverlight_xap_url |
||
| 1121 | }; |
||
| 1122 | |||
| 1123 | // add runtime specific options if any |
||
| 1124 | plupload.each(settings.runtimes.split(/\s*,\s*/), function(runtime) { |
||
| 1125 | if (settings[runtime]) { |
||
| 1126 | options[runtime] = settings[runtime]; |
||
| 1127 | } |
||
| 1128 | }); |
||
| 1129 | |||
| 1130 | // initialize file pickers - there can be many |
||
| 1131 | if (settings.browse_button) { |
||
| 1132 | plupload.each(settings.browse_button, function(el) { |
||
| 1133 | queue.push(function(cb) { |
||
| 1134 | var fileInput = new o.file.FileInput(plupload.extend({}, options, { |
||
| 1135 | accept: settings.filters.mime_types, |
||
| 1136 | name: settings.file_data_name, |
||
| 1137 | multiple: settings.multi_selection, |
||
| 1138 | container: settings.container, |
||
| 1139 | browse_button: el |
||
| 1140 | })); |
||
| 1141 | |||
| 1142 | fileInput.onready = function() { |
||
| 1143 | var info = Runtime.getInfo(this.ruid); |
||
| 1144 | |||
| 1145 | // for backward compatibility |
||
| 1146 | plupload.extend(self.features, { |
||
| 1147 | chunks: info.can('slice_blob'), |
||
| 1148 | multipart: info.can('send_multipart'), |
||
| 1149 | multi_selection: info.can('select_multiple') |
||
| 1150 | }); |
||
| 1151 | |||
| 1152 | inited++; |
||
| 1153 | fileInputs.push(this); |
||
| 1154 | cb(); |
||
| 1155 | }; |
||
| 1156 | |||
| 1157 | fileInput.onchange = function() { |
||
| 1158 | self.addFile(this.files); |
||
| 1159 | }; |
||
| 1160 | |||
| 1161 | fileInput.bind('mouseenter mouseleave mousedown mouseup', function(e) { |
||
| 1162 | if (!disabled) { |
||
| 1163 | if (settings.browse_button_hover) { |
||
| 1164 | if ('mouseenter' === e.type) { |
||
| 1165 | plupload.addClass(el, settings.browse_button_hover); |
||
| 1166 | } else if ('mouseleave' === e.type) { |
||
| 1167 | plupload.removeClass(el, settings.browse_button_hover); |
||
| 1168 | } |
||
| 1169 | } |
||
| 1170 | |||
| 1171 | if (settings.browse_button_active) { |
||
| 1172 | if ('mousedown' === e.type) { |
||
| 1173 | plupload.addClass(el, settings.browse_button_active); |
||
| 1174 | } else if ('mouseup' === e.type) { |
||
| 1175 | plupload.removeClass(el, settings.browse_button_active); |
||
| 1176 | } |
||
| 1177 | } |
||
| 1178 | } |
||
| 1179 | }); |
||
| 1180 | |||
| 1181 | fileInput.bind('mousedown', function() { |
||
| 1182 | self.trigger('Browse'); |
||
| 1183 | }); |
||
| 1184 | |||
| 1185 | fileInput.bind('error runtimeerror', function() { |
||
| 1186 | fileInput = null; |
||
| 1187 | cb(); |
||
| 1188 | }); |
||
| 1189 | |||
| 1190 | fileInput.init(); |
||
| 1191 | }); |
||
| 1192 | }); |
||
| 1193 | } |
||
| 1194 | |||
| 1195 | // initialize drop zones |
||
| 1196 | if (settings.drop_element) { |
||
| 1197 | plupload.each(settings.drop_element, function(el) { |
||
| 1198 | queue.push(function(cb) { |
||
| 1199 | var fileDrop = new o.file.FileDrop(plupload.extend({}, options, { |
||
| 1200 | drop_zone: el |
||
| 1201 | })); |
||
| 1202 | |||
| 1203 | fileDrop.onready = function() { |
||
| 1204 | var info = Runtime.getInfo(this.ruid); |
||
| 1205 | |||
| 1206 | // for backward compatibility |
||
| 1207 | plupload.extend(self.features, { |
||
| 1208 | chunks: info.can('slice_blob'), |
||
| 1209 | multipart: info.can('send_multipart'), |
||
| 1210 | dragdrop: info.can('drag_and_drop') |
||
| 1211 | }); |
||
| 1212 | |||
| 1213 | inited++; |
||
| 1214 | fileDrops.push(this); |
||
| 1215 | cb(); |
||
| 1216 | }; |
||
| 1217 | |||
| 1218 | fileDrop.ondrop = function() { |
||
| 1219 | self.addFile(this.files); |
||
| 1220 | }; |
||
| 1221 | |||
| 1222 | fileDrop.bind('error runtimeerror', function() { |
||
| 1223 | fileDrop = null; |
||
| 1224 | cb(); |
||
| 1225 | }); |
||
| 1226 | |||
| 1227 | fileDrop.init(); |
||
| 1228 | }); |
||
| 1229 | }); |
||
| 1230 | } |
||
| 1231 | |||
| 1232 | |||
| 1233 | plupload.inSeries(queue, function() { |
||
| 1234 | if (typeof(cb) === 'function') { |
||
| 1235 | cb(inited); |
||
| 1236 | } |
||
| 1237 | }); |
||
| 1238 | } |
||
| 1239 | |||
| 1240 | |||
| 1241 | function resizeImage(blob, params, runtimeOptions, cb) { |
||
| 1242 | var img = new o.image.Image(); |
||
| 1243 | |||
| 1244 | try { |
||
| 1245 | img.onload = function() { |
||
| 1246 | // no manipulation required if... |
||
| 1247 | if (params.width > this.width && |
||
| 1248 | params.height > this.height && |
||
| 1249 | params.quality === undef && |
||
| 1250 | params.preserve_headers && |
||
| 1251 | !params.crop |
||
| 1252 | ) { |
||
| 1253 | this.destroy(); |
||
| 1254 | cb(blob); |
||
| 1255 | } else { |
||
| 1256 | // otherwise downsize |
||
| 1257 | img.downsize(params.width, params.height, params.crop, params.preserve_headers); |
||
| 1258 | } |
||
| 1259 | }; |
||
| 1260 | |||
| 1261 | img.onresize = function() { |
||
| 1262 | var resizedBlob = this.getAsBlob(blob.type, params.quality); |
||
| 1263 | this.destroy(); |
||
| 1264 | cb(resizedBlob); |
||
| 1265 | }; |
||
| 1266 | |||
| 1267 | img.bind('error runtimeerror', function() { |
||
| 1268 | this.destroy(); |
||
| 1269 | cb(blob); |
||
| 1270 | }); |
||
| 1271 | |||
| 1272 | img.load(blob, runtimeOptions); |
||
| 1273 | } catch(ex) { |
||
| 1274 | cb(blob); |
||
| 1275 | } |
||
| 1276 | } |
||
| 1277 | |||
| 1278 | |||
| 1279 | function setOption(option, value, init) { |
||
| 1280 | var self = this, reinitRequired = false; |
||
| 1281 | |||
| 1282 | function _setOption(option, value, init) { |
||
| 1283 | var oldValue = settings[option]; |
||
| 1284 | |||
| 1285 | switch (option) { |
||
| 1286 | case 'max_file_size': |
||
| 1287 | if (option === 'max_file_size') { |
||
| 1288 | settings.max_file_size = settings.filters.max_file_size = value; |
||
| 1289 | } |
||
| 1290 | break; |
||
| 1291 | |||
| 1292 | case 'chunk_size': |
||
| 1293 | if (value = plupload.parseSize(value)) { |
||
| 1294 | settings[option] = value; |
||
| 1295 | settings.send_file_name = true; |
||
| 1296 | } |
||
| 1297 | break; |
||
| 1298 | |||
| 1299 | case 'multipart': |
||
| 1300 | settings[option] = value; |
||
| 1301 | if (!value) { |
||
| 1302 | settings.send_file_name = true; |
||
| 1303 | } |
||
| 1304 | break; |
||
| 1305 | |||
| 1306 | case 'http_method': |
||
| 1307 | settings[option] = value.toUpperCase() === 'PUT' ? 'PUT' : 'POST'; |
||
| 1308 | break; |
||
| 1309 | |||
| 1310 | case 'unique_names': |
||
| 1311 | settings[option] = value; |
||
| 1312 | if (value) { |
||
| 1313 | settings.send_file_name = true; |
||
| 1314 | } |
||
| 1315 | break; |
||
| 1316 | |||
| 1317 | case 'filters': |
||
| 1318 | // for sake of backward compatibility |
||
| 1319 | if (plupload.typeOf(value) === 'array') { |
||
| 1320 | value = { |
||
| 1321 | mime_types: value |
||
| 1322 | }; |
||
| 1323 | } |
||
| 1324 | |||
| 1325 | if (init) { |
||
| 1326 | plupload.extend(settings.filters, value); |
||
| 1327 | } else { |
||
| 1328 | settings.filters = value; |
||
| 1329 | } |
||
| 1330 | |||
| 1331 | // if file format filters are being updated, regenerate the matching expressions |
||
| 1332 | if (value.mime_types) { |
||
| 1333 | if (plupload.typeOf(value.mime_types) === 'string') { |
||
| 1334 | value.mime_types = o.core.utils.Mime.mimes2extList(value.mime_types); |
||
| 1335 | } |
||
| 1336 | |||
| 1337 | value.mime_types.regexp = (function(filters) { |
||
| 1338 | var extensionsRegExp = []; |
||
| 1339 | |||
| 1340 | plupload.each(filters, function(filter) { |
||
| 1341 | plupload.each(filter.extensions.split(/,/), function(ext) { |
||
| 1342 | if (/^\s*\*\s*$/.test(ext)) { |
||
| 1343 | extensionsRegExp.push('\\.*'); |
||
| 1344 | } else { |
||
| 1345 | extensionsRegExp.push('\\.' + ext.replace(new RegExp('[' + ('/^$.*+?|()[]{}\\'.replace(/./g, '\\$&')) + ']', 'g'), '\\$&')); |
||
| 1346 | } |
||
| 1347 | }); |
||
| 1348 | }); |
||
| 1349 | |||
| 1350 | return new RegExp('(' + extensionsRegExp.join('|') + ')$', 'i'); |
||
| 1351 | }(value.mime_types)); |
||
| 1352 | |||
| 1353 | settings.filters.mime_types = value.mime_types; |
||
| 1354 | } |
||
| 1355 | break; |
||
| 1356 | |||
| 1357 | case 'resize': |
||
| 1358 | if (value) { |
||
| 1359 | settings.resize = plupload.extend({ |
||
| 1360 | preserve_headers: true, |
||
| 1361 | crop: false |
||
| 1362 | }, value); |
||
| 1363 | } else { |
||
| 1364 | settings.resize = false; |
||
| 1365 | } |
||
| 1366 | break; |
||
| 1367 | |||
| 1368 | case 'prevent_duplicates': |
||
| 1369 | settings.prevent_duplicates = settings.filters.prevent_duplicates = !!value; |
||
| 1370 | break; |
||
| 1371 | |||
| 1372 | // options that require reinitialisation |
||
| 1373 | case 'container': |
||
| 1374 | case 'browse_button': |
||
| 1375 | case 'drop_element': |
||
| 1376 | value = 'container' === option |
||
| 1377 | ? plupload.get(value) |
||
| 1378 | : plupload.getAll(value) |
||
| 1379 | ; |
||
| 1380 | |||
| 1381 | case 'runtimes': |
||
| 1382 | case 'multi_selection': |
||
| 1383 | case 'flash_swf_url': |
||
| 1384 | case 'silverlight_xap_url': |
||
| 1385 | settings[option] = value; |
||
| 1386 | if (!init) { |
||
| 1387 | reinitRequired = true; |
||
| 1388 | } |
||
| 1389 | break; |
||
| 1390 | |||
| 1391 | default: |
||
| 1392 | settings[option] = value; |
||
| 1393 | } |
||
| 1394 | |||
| 1395 | if (!init) { |
||
| 1396 | self.trigger('OptionChanged', option, value, oldValue); |
||
| 1397 | } |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | if (typeof(option) === 'object') { |
||
| 1401 | plupload.each(option, function(value, option) { |
||
| 1402 | _setOption(option, value, init); |
||
| 1403 | }); |
||
| 1404 | } else { |
||
| 1405 | _setOption(option, value, init); |
||
| 1406 | } |
||
| 1407 | |||
| 1408 | if (init) { |
||
| 1409 | // Normalize the list of required capabilities |
||
| 1410 | settings.required_features = normalizeCaps(plupload.extend({}, settings)); |
||
| 1411 | |||
| 1412 | // Come up with the list of capabilities that can affect default mode in a multi-mode runtimes |
||
| 1413 | preferred_caps = normalizeCaps(plupload.extend({}, settings, { |
||
| 1414 | required_features: true |
||
| 1415 | })); |
||
| 1416 | } else if (reinitRequired) { |
||
| 1417 | self.trigger('Destroy'); |
||
| 1418 | |||
| 1419 | initControls.call(self, settings, function(inited) { |
||
| 1420 | if (inited) { |
||
| 1421 | self.runtime = Runtime.getInfo(getRUID()).type; |
||
| 1422 | self.trigger('Init', { runtime: self.runtime }); |
||
| 1423 | self.trigger('PostInit'); |
||
| 1424 | } else { |
||
| 1425 | self.trigger('Error', { |
||
| 1426 | code : plupload.INIT_ERROR, |
||
| 1427 | message : plupload.translate('Init error.') |
||
| 1428 | }); |
||
| 1429 | } |
||
| 1430 | }); |
||
| 1431 | } |
||
| 1432 | } |
||
| 1433 | |||
| 1434 | |||
| 1435 | // Internal event handlers |
||
| 1436 | function onBeforeUpload(up, file) { |
||
| 1437 | // Generate unique target filenames |
||
| 1438 | if (up.settings.unique_names) { |
||
| 1439 | var matches = file.name.match(/\.([^.]+)$/), ext = "part"; |
||
| 1440 | if (matches) { |
||
| 1441 | ext = matches[1]; |
||
| 1442 | } |
||
| 1443 | file.target_name = file.id + '.' + ext; |
||
| 1444 | } |
||
| 1445 | } |
||
| 1446 | |||
| 1447 | |||
| 1448 | function onUploadFile(up, file) { |
||
| 1449 | var url = up.settings.url; |
||
| 1450 | var chunkSize = up.settings.chunk_size; |
||
| 1451 | var retries = up.settings.max_retries; |
||
| 1452 | var features = up.features; |
||
| 1453 | var offset = 0; |
||
| 1454 | var blob; |
||
| 1455 | |||
| 1456 | var runtimeOptions = { |
||
| 1457 | runtime_order: up.settings.runtimes, |
||
| 1458 | required_caps: up.settings.required_features, |
||
| 1459 | preferred_caps: preferred_caps, |
||
| 1460 | swf_url: up.settings.flash_swf_url, |
||
| 1461 | xap_url: up.settings.silverlight_xap_url |
||
| 1462 | }; |
||
| 1463 | |||
| 1464 | // make sure we start at a predictable offset |
||
| 1465 | if (file.loaded) { |
||
| 1466 | offset = file.loaded = chunkSize ? chunkSize * Math.floor(file.loaded / chunkSize) : 0; |
||
| 1467 | } |
||
| 1468 | |||
| 1469 | function handleError() { |
||
| 1470 | if (retries-- > 0) { |
||
| 1471 | delay(uploadNextChunk, 1000); |
||
| 1472 | } else { |
||
| 1473 | file.loaded = offset; // reset all progress |
||
| 1474 | |||
| 1475 | up.trigger('Error', { |
||
| 1476 | code : plupload.HTTP_ERROR, |
||
| 1477 | message : plupload.translate('HTTP Error.'), |
||
| 1478 | file : file, |
||
| 1479 | response : xhr.responseText, |
||
| 1480 | status : xhr.status, |
||
| 1481 | responseHeaders: xhr.getAllResponseHeaders() |
||
| 1482 | }); |
||
| 1483 | } |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | function uploadNextChunk() { |
||
| 1487 | var chunkBlob, args = {}, curChunkSize; |
||
| 1488 | |||
| 1489 | // make sure that file wasn't cancelled and upload is not stopped in general |
||
| 1490 | if (file.status !== plupload.UPLOADING || up.state === plupload.STOPPED) { |
||
| 1491 | return; |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | // send additional 'name' parameter only if required |
||
| 1495 | if (up.settings.send_file_name) { |
||
| 1496 | args.name = file.target_name || file.name; |
||
| 1497 | } |
||
| 1498 | |||
| 1499 | if (chunkSize && features.chunks && blob.size > chunkSize) { // blob will be of type string if it was loaded in memory |
||
| 1500 | curChunkSize = Math.min(chunkSize, blob.size - offset); |
||
| 1501 | chunkBlob = blob.slice(offset, offset + curChunkSize); |
||
| 1502 | } else { |
||
| 1503 | curChunkSize = blob.size; |
||
| 1504 | chunkBlob = blob; |
||
| 1505 | } |
||
| 1506 | |||
| 1507 | // If chunking is enabled add corresponding args, no matter if file is bigger than chunk or smaller |
||
| 1508 | if (chunkSize && features.chunks) { |
||
| 1509 | // Setup query string arguments |
||
| 1510 | if (up.settings.send_chunk_number) { |
||
| 1511 | args.chunk = Math.ceil(offset / chunkSize); |
||
| 1512 | args.chunks = Math.ceil(blob.size / chunkSize); |
||
| 1513 | } else { // keep support for experimental chunk format, just in case |
||
| 1514 | args.offset = offset; |
||
| 1515 | args.total = blob.size; |
||
| 1516 | } |
||
| 1517 | } |
||
| 1518 | |||
| 1519 | if (up.trigger('BeforeChunkUpload', file, args, chunkBlob, offset)) { |
||
| 1520 | uploadChunk(args, chunkBlob, curChunkSize); |
||
| 1521 | } |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | function uploadChunk(args, chunkBlob, curChunkSize) { |
||
| 1525 | var formData; |
||
| 1526 | |||
| 1527 | xhr = new o.xhr.XMLHttpRequest(); |
||
| 1528 | |||
| 1529 | // Do we have upload progress support |
||
| 1530 | if (xhr.upload) { |
||
| 1531 | xhr.upload.onprogress = function(e) { |
||
| 1532 | file.loaded = Math.min(file.size, offset + e.loaded); |
||
| 1533 | up.trigger('UploadProgress', file); |
||
| 1534 | }; |
||
| 1535 | } |
||
| 1536 | |||
| 1537 | xhr.onload = function() { |
||
| 1538 | // check if upload made itself through |
||
| 1539 | if (xhr.status < 200 || xhr.status >= 400) { |
||
| 1540 | handleError(); |
||
| 1541 | return; |
||
| 1542 | } |
||
| 1543 | |||
| 1544 | retries = up.settings.max_retries; // reset the counter |
||
| 1545 | |||
| 1546 | // Handle chunk response |
||
| 1547 | if (curChunkSize < blob.size) { |
||
| 1548 | chunkBlob.destroy(); |
||
| 1549 | |||
| 1550 | offset += curChunkSize; |
||
| 1551 | file.loaded = Math.min(offset, blob.size); |
||
| 1552 | |||
| 1553 | up.trigger('ChunkUploaded', file, { |
||
| 1554 | offset : file.loaded, |
||
| 1555 | total : blob.size, |
||
| 1556 | response : xhr.responseText, |
||
| 1557 | status : xhr.status, |
||
| 1558 | responseHeaders: xhr.getAllResponseHeaders() |
||
| 1559 | }); |
||
| 1560 | |||
| 1561 | // stock Android browser doesn't fire upload progress events, but in chunking mode we can fake them |
||
| 1562 | if (plupload.ua.browser === 'Android Browser') { |
||
| 1563 | // doesn't harm in general, but is not required anywhere else |
||
| 1564 | up.trigger('UploadProgress', file); |
||
| 1565 | } |
||
| 1566 | } else { |
||
| 1567 | file.loaded = file.size; |
||
| 1568 | } |
||
| 1569 | |||
| 1570 | chunkBlob = formData = null; // Free memory |
||
| 1571 | |||
| 1572 | // Check if file is uploaded |
||
| 1573 | if (!offset || offset >= blob.size) { |
||
| 1574 | // If file was modified, destory the copy |
||
| 1575 | if (file.size != file.origSize) { |
||
| 1576 | blob.destroy(); |
||
| 1577 | blob = null; |
||
| 1578 | } |
||
| 1579 | |||
| 1580 | up.trigger('UploadProgress', file); |
||
| 1581 | |||
| 1582 | file.status = plupload.DONE; |
||
| 1583 | file.completeTimestamp = +new Date(); |
||
| 1584 | |||
| 1585 | up.trigger('FileUploaded', file, { |
||
| 1586 | response : xhr.responseText, |
||
| 1587 | status : xhr.status, |
||
| 1588 | responseHeaders: xhr.getAllResponseHeaders() |
||
| 1589 | }); |
||
| 1590 | } else { |
||
| 1591 | // Still chunks left |
||
| 1592 | delay(uploadNextChunk, 1); // run detached, otherwise event handlers interfere |
||
| 1593 | } |
||
| 1594 | }; |
||
| 1595 | |||
| 1596 | xhr.onerror = function() { |
||
| 1597 | handleError(); |
||
| 1598 | }; |
||
| 1599 | |||
| 1600 | xhr.onloadend = function() { |
||
| 1601 | this.destroy(); |
||
| 1602 | }; |
||
| 1603 | |||
| 1604 | // Build multipart request |
||
| 1605 | if (up.settings.multipart && features.multipart) { |
||
| 1606 | xhr.open(up.settings.http_method, url, true); |
||
| 1607 | |||
| 1608 | // Set custom headers |
||
| 1609 | plupload.each(up.settings.headers, function(value, name) { |
||
| 1610 | xhr.setRequestHeader(name, value); |
||
| 1611 | }); |
||
| 1612 | |||
| 1613 | formData = new o.xhr.FormData(); |
||
| 1614 | |||
| 1615 | // Add multipart params |
||
| 1616 | plupload.each(plupload.extend(args, up.settings.multipart_params), function(value, name) { |
||
| 1617 | formData.append(name, value); |
||
| 1618 | }); |
||
| 1619 | |||
| 1620 | // Add file and send it |
||
| 1621 | formData.append(up.settings.file_data_name, chunkBlob); |
||
| 1622 | xhr.send(formData, runtimeOptions); |
||
| 1623 | } else { |
||
| 1624 | // if no multipart, send as binary stream |
||
| 1625 | url = plupload.buildUrl(up.settings.url, plupload.extend(args, up.settings.multipart_params)); |
||
| 1626 | |||
| 1627 | xhr.open(up.settings.http_method, url, true); |
||
| 1628 | |||
| 1629 | // Set custom headers |
||
| 1630 | plupload.each(up.settings.headers, function(value, name) { |
||
| 1631 | xhr.setRequestHeader(name, value); |
||
| 1632 | }); |
||
| 1633 | |||
| 1634 | // do not set Content-Type, if it was defined previously (see #1203) |
||
| 1635 | if (!xhr.hasRequestHeader('Content-Type')) { |
||
| 1636 | xhr.setRequestHeader('Content-Type', 'application/octet-stream'); // Binary stream header |
||
| 1637 | } |
||
| 1638 | |||
| 1639 | xhr.send(chunkBlob, runtimeOptions); |
||
| 1640 | } |
||
| 1641 | } |
||
| 1642 | |||
| 1643 | |||
| 1644 | blob = file.getSource(); |
||
| 1645 | |||
| 1646 | // Start uploading chunks |
||
| 1647 | if (!plupload.isEmptyObj(up.settings.resize) && plupload.inArray(blob.type, ['image/jpeg', 'image/png']) !== -1) { |
||
| 1648 | // Resize if required |
||
| 1649 | resizeImage(blob, up.settings.resize, runtimeOptions, function(resizedBlob) { |
||
| 1650 | blob = resizedBlob; |
||
| 1651 | file.size = resizedBlob.size; |
||
| 1652 | uploadNextChunk(); |
||
| 1653 | }); |
||
| 1654 | } else { |
||
| 1655 | uploadNextChunk(); |
||
| 1656 | } |
||
| 1657 | } |
||
| 1658 | |||
| 1659 | |||
| 1660 | function onUploadProgress(up, file) { |
||
| 1661 | calcFile(file); |
||
| 1662 | } |
||
| 1663 | |||
| 1664 | |||
| 1665 | function onStateChanged(up) { |
||
| 1666 | if (up.state == plupload.STARTED) { |
||
| 1667 | // Get start time to calculate bps |
||
| 1668 | startTime = (+new Date()); |
||
| 1669 | } else if (up.state == plupload.STOPPED) { |
||
| 1670 | // Reset currently uploading files |
||
| 1671 | for (var i = up.files.length - 1; i >= 0; i--) { |
||
| 1672 | if (up.files[i].status == plupload.UPLOADING) { |
||
| 1673 | up.files[i].status = plupload.QUEUED; |
||
| 1674 | calc(); |
||
| 1675 | } |
||
| 1676 | } |
||
| 1677 | } |
||
| 1678 | } |
||
| 1679 | |||
| 1680 | |||
| 1681 | function onCancelUpload() { |
||
| 1682 | if (xhr) { |
||
| 1683 | xhr.abort(); |
||
| 1684 | } |
||
| 1685 | } |
||
| 1686 | |||
| 1687 | |||
| 1688 | function onFileUploaded(up) { |
||
| 1689 | calc(); |
||
| 1690 | |||
| 1691 | // Upload next file but detach it from the error event |
||
| 1692 | // since other custom listeners might want to stop the queue |
||
| 1693 | delay(function() { |
||
| 1694 | uploadNext.call(up); |
||
| 1695 | }, 1); |
||
| 1696 | } |
||
| 1697 | |||
| 1698 | |||
| 1699 | function onError(up, err) { |
||
| 1700 | if (err.code === plupload.INIT_ERROR) { |
||
| 1701 | up.destroy(); |
||
| 1702 | } |
||
| 1703 | // Set failed status if an error occured on a file |
||
| 1704 | else if (err.code === plupload.HTTP_ERROR) { |
||
| 1705 | err.file.status = plupload.FAILED; |
||
| 1706 | err.file.completeTimestamp = +new Date(); |
||
| 1707 | calcFile(err.file); |
||
| 1708 | |||
| 1709 | // Upload next file but detach it from the error event |
||
| 1710 | // since other custom listeners might want to stop the queue |
||
| 1711 | if (up.state == plupload.STARTED) { // upload in progress |
||
| 1712 | up.trigger('CancelUpload'); |
||
| 1713 | delay(function() { |
||
| 1714 | uploadNext.call(up); |
||
| 1715 | }, 1); |
||
| 1716 | } |
||
| 1717 | } |
||
| 1718 | } |
||
| 1719 | |||
| 1720 | |||
| 1721 | function onDestroy(up) { |
||
| 1722 | up.stop(); |
||
| 1723 | |||
| 1724 | // Purge the queue |
||
| 1725 | plupload.each(files, function(file) { |
||
| 1726 | file.destroy(); |
||
| 1727 | }); |
||
| 1728 | files = []; |
||
| 1729 | |||
| 1730 | if (fileInputs.length) { |
||
| 1731 | plupload.each(fileInputs, function(fileInput) { |
||
| 1732 | fileInput.destroy(); |
||
| 1733 | }); |
||
| 1734 | fileInputs = []; |
||
| 1735 | } |
||
| 1736 | |||
| 1737 | if (fileDrops.length) { |
||
| 1738 | plupload.each(fileDrops, function(fileDrop) { |
||
| 1739 | fileDrop.destroy(); |
||
| 1740 | }); |
||
| 1741 | fileDrops = []; |
||
| 1742 | } |
||
| 1743 | |||
| 1744 | preferred_caps = {}; |
||
| 1745 | disabled = false; |
||
| 1746 | startTime = xhr = null; |
||
| 1747 | total.reset(); |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | |||
| 1751 | // Default settings |
||
| 1752 | settings = { |
||
| 1753 | chunk_size: 0, |
||
| 1754 | file_data_name: 'file', |
||
| 1755 | filters: { |
||
| 1756 | mime_types: [], |
||
| 1757 | max_file_size: 0, |
||
| 1758 | prevent_duplicates: false, |
||
| 1759 | prevent_empty: true |
||
| 1760 | }, |
||
| 1761 | flash_swf_url: 'js/Moxie.swf', |
||
| 1762 | http_method: 'POST', |
||
| 1763 | max_retries: 0, |
||
| 1764 | multipart: true, |
||
| 1765 | multi_selection: true, |
||
| 1766 | resize: false, |
||
| 1767 | runtimes: Runtime.order, |
||
| 1768 | send_file_name: true, |
||
| 1769 | send_chunk_number: true, |
||
| 1770 | silverlight_xap_url: 'js/Moxie.xap' |
||
| 1771 | }; |
||
| 1772 | |||
| 1773 | |||
| 1774 | setOption.call(this, options, null, true); |
||
| 1775 | |||
| 1776 | // Inital total state |
||
| 1777 | total = new plupload.QueueProgress(); |
||
| 1778 | |||
| 1779 | // Add public methods |
||
| 1780 | plupload.extend(this, { |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Unique id for the Uploader instance. |
||
| 1784 | * |
||
| 1785 | * @property id |
||
| 1786 | * @type String |
||
| 1787 | */ |
||
| 1788 | id : uid, |
||
| 1789 | uid : uid, // mOxie uses this to differentiate between event targets |
||
| 1790 | |||
| 1791 | /** |
||
| 1792 | * Current state of the total uploading progress. This one can either be plupload.STARTED or plupload.STOPPED. |
||
| 1793 | * These states are controlled by the stop/start methods. The default value is STOPPED. |
||
| 1794 | * |
||
| 1795 | * @property state |
||
| 1796 | * @type Number |
||
| 1797 | */ |
||
| 1798 | state : plupload.STOPPED, |
||
| 1799 | |||
| 1800 | /** |
||
| 1801 | * Map of features that are available for the uploader runtime. Features will be filled |
||
| 1802 | * before the init event is called, these features can then be used to alter the UI for the end user. |
||
| 1803 | * Some of the current features that might be in this map is: dragdrop, chunks, jpgresize, pngresize. |
||
| 1804 | * |
||
| 1805 | * @property features |
||
| 1806 | * @type Object |
||
| 1807 | */ |
||
| 1808 | features : {}, |
||
| 1809 | |||
| 1810 | /** |
||
| 1811 | * Current runtime name. |
||
| 1812 | * |
||
| 1813 | * @property runtime |
||
| 1814 | * @type String |
||
| 1815 | */ |
||
| 1816 | runtime : null, |
||
| 1817 | |||
| 1818 | /** |
||
| 1819 | * Current upload queue, an array of File instances. |
||
| 1820 | * |
||
| 1821 | * @property files |
||
| 1822 | * @type Array |
||
| 1823 | * @see plupload.File |
||
| 1824 | */ |
||
| 1825 | files : files, |
||
| 1826 | |||
| 1827 | /** |
||
| 1828 | * Object with name/value settings. |
||
| 1829 | * |
||
| 1830 | * @property settings |
||
| 1831 | * @type Object |
||
| 1832 | */ |
||
| 1833 | settings : settings, |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Total progess information. How many files has been uploaded, total percent etc. |
||
| 1837 | * |
||
| 1838 | * @property total |
||
| 1839 | * @type plupload.QueueProgress |
||
| 1840 | */ |
||
| 1841 | total : total, |
||
| 1842 | |||
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Initializes the Uploader instance and adds internal event listeners. |
||
| 1846 | * |
||
| 1847 | * @method init |
||
| 1848 | */ |
||
| 1849 | init : function() { |
||
| 1850 | var self = this, opt, preinitOpt, err; |
||
| 1851 | |||
| 1852 | preinitOpt = self.getOption('preinit'); |
||
| 1853 | if (typeof(preinitOpt) == "function") { |
||
| 1854 | preinitOpt(self); |
||
| 1855 | } else { |
||
| 1856 | plupload.each(preinitOpt, function(func, name) { |
||
| 1857 | self.bind(name, func); |
||
| 1858 | }); |
||
| 1859 | } |
||
| 1860 | |||
| 1861 | bindEventListeners.call(self); |
||
| 1862 | |||
| 1863 | // Check for required options |
||
| 1864 | plupload.each(['container', 'browse_button', 'drop_element'], function(el) { |
||
| 1865 | if (self.getOption(el) === null) { |
||
| 1866 | err = { |
||
| 1867 | code : plupload.INIT_ERROR, |
||
| 1868 | message : plupload.sprintf(plupload.translate("%s specified, but cannot be found."), el) |
||
| 1869 | } |
||
| 1870 | return false; |
||
| 1871 | } |
||
| 1872 | }); |
||
| 1873 | |||
| 1874 | if (err) { |
||
| 1875 | return self.trigger('Error', err); |
||
| 1876 | } |
||
| 1877 | |||
| 1878 | |||
| 1879 | if (!settings.browse_button && !settings.drop_element) { |
||
| 1880 | return self.trigger('Error', { |
||
| 1881 | code : plupload.INIT_ERROR, |
||
| 1882 | message : plupload.translate("You must specify either browse_button or drop_element.") |
||
| 1883 | }); |
||
| 1884 | } |
||
| 1885 | |||
| 1886 | |||
| 1887 | initControls.call(self, settings, function(inited) { |
||
| 1888 | var initOpt = self.getOption('init'); |
||
| 1889 | if (typeof(initOpt) == "function") { |
||
| 1890 | initOpt(self); |
||
| 1891 | } else { |
||
| 1892 | plupload.each(initOpt, function(func, name) { |
||
| 1893 | self.bind(name, func); |
||
| 1894 | }); |
||
| 1895 | } |
||
| 1896 | |||
| 1897 | if (inited) { |
||
| 1898 | self.runtime = Runtime.getInfo(getRUID()).type; |
||
| 1899 | self.trigger('Init', { runtime: self.runtime }); |
||
| 1900 | self.trigger('PostInit'); |
||
| 1901 | } else { |
||
| 1902 | self.trigger('Error', { |
||
| 1903 | code : plupload.INIT_ERROR, |
||
| 1904 | message : plupload.translate('Init error.') |
||
| 1905 | }); |
||
| 1906 | } |
||
| 1907 | }); |
||
| 1908 | }, |
||
| 1909 | |||
| 1910 | /** |
||
| 1911 | * Set the value for the specified option(s). |
||
| 1912 | * |
||
| 1913 | * @method setOption |
||
| 1914 | * @since 2.1 |
||
| 1915 | * @param {String|Object} option Name of the option to change or the set of key/value pairs |
||
| 1916 | * @param {Mixed} [value] Value for the option (is ignored, if first argument is object) |
||
| 1917 | */ |
||
| 1918 | setOption: function(option, value) { |
||
| 1919 | setOption.call(this, option, value, !this.runtime); // until runtime not set we do not need to reinitialize |
||
| 1920 | }, |
||
| 1921 | |||
| 1922 | /** |
||
| 1923 | * Get the value for the specified option or the whole configuration, if not specified. |
||
| 1924 | * |
||
| 1925 | * @method getOption |
||
| 1926 | * @since 2.1 |
||
| 1927 | * @param {String} [option] Name of the option to get |
||
| 1928 | * @return {Mixed} Value for the option or the whole set |
||
| 1929 | */ |
||
| 1930 | getOption: function(option) { |
||
| 1931 | if (!option) { |
||
| 1932 | return settings; |
||
| 1933 | } |
||
| 1934 | return settings[option]; |
||
| 1935 | }, |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Refreshes the upload instance by dispatching out a refresh event to all runtimes. |
||
| 1939 | * This would for example reposition flash/silverlight shims on the page. |
||
| 1940 | * |
||
| 1941 | * @method refresh |
||
| 1942 | */ |
||
| 1943 | refresh : function() { |
||
| 1944 | if (fileInputs.length) { |
||
| 1945 | plupload.each(fileInputs, function(fileInput) { |
||
| 1946 | fileInput.trigger('Refresh'); |
||
| 1947 | }); |
||
| 1948 | } |
||
| 1949 | this.trigger('Refresh'); |
||
| 1950 | }, |
||
| 1951 | |||
| 1952 | /** |
||
| 1953 | * Starts uploading the queued files. |
||
| 1954 | * |
||
| 1955 | * @method start |
||
| 1956 | */ |
||
| 1957 | start : function() { |
||
| 1958 | if (this.state != plupload.STARTED) { |
||
| 1959 | this.state = plupload.STARTED; |
||
| 1960 | this.trigger('StateChanged'); |
||
| 1961 | |||
| 1962 | uploadNext.call(this); |
||
| 1963 | } |
||
| 1964 | }, |
||
| 1965 | |||
| 1966 | /** |
||
| 1967 | * Stops the upload of the queued files. |
||
| 1968 | * |
||
| 1969 | * @method stop |
||
| 1970 | */ |
||
| 1971 | stop : function() { |
||
| 1972 | if (this.state != plupload.STOPPED) { |
||
| 1973 | this.state = plupload.STOPPED; |
||
| 1974 | this.trigger('StateChanged'); |
||
| 1975 | this.trigger('CancelUpload'); |
||
| 1976 | } |
||
| 1977 | }, |
||
| 1978 | |||
| 1979 | |||
| 1980 | /** |
||
| 1981 | * Disables/enables browse button on request. |
||
| 1982 | * |
||
| 1983 | * @method disableBrowse |
||
| 1984 | * @param {Boolean} disable Whether to disable or enable (default: true) |
||
| 1985 | */ |
||
| 1986 | disableBrowse : function() { |
||
| 1987 | disabled = arguments[0] !== undef ? arguments[0] : true; |
||
| 1988 | |||
| 1989 | if (fileInputs.length) { |
||
| 1990 | plupload.each(fileInputs, function(fileInput) { |
||
| 1991 | fileInput.disable(disabled); |
||
| 1992 | }); |
||
| 1993 | } |
||
| 1994 | |||
| 1995 | this.trigger('DisableBrowse', disabled); |
||
| 1996 | }, |
||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * Returns the specified file object by id. |
||
| 2000 | * |
||
| 2001 | * @method getFile |
||
| 2002 | * @param {String} id File id to look for. |
||
| 2003 | * @return {plupload.File} File object or undefined if it wasn't found; |
||
| 2004 | */ |
||
| 2005 | getFile : function(id) { |
||
| 2006 | var i; |
||
| 2007 | for (i = files.length - 1; i >= 0; i--) { |
||
| 2008 | if (files[i].id === id) { |
||
| 2009 | return files[i]; |
||
| 2010 | } |
||
| 2011 | } |
||
| 2012 | }, |
||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Adds file to the queue programmatically. Can be native file, instance of Plupload.File, |
||
| 2016 | * instance of mOxie.File, input[type="file"] element, or array of these. Fires FilesAdded, |
||
| 2017 | * if any files were added to the queue. Otherwise nothing happens. |
||
| 2018 | * |
||
| 2019 | * @method addFile |
||
| 2020 | * @since 2.0 |
||
| 2021 | * @param {plupload.File|mOxie.File|File|Node|Array} file File or files to add to the queue. |
||
| 2022 | * @param {String} [fileName] If specified, will be used as a name for the file |
||
| 2023 | */ |
||
| 2024 | addFile : function(file, fileName) { |
||
| 2025 | var self = this |
||
| 2026 | , queue = [] |
||
| 2027 | , filesAdded = [] |
||
| 2028 | , ruid |
||
| 2029 | ; |
||
| 2030 | |||
| 2031 | function filterFile(file, cb) { |
||
| 2032 | var queue = []; |
||
| 2033 | plupload.each(self.settings.filters, function(rule, name) { |
||
| 2034 | if (fileFilters[name]) { |
||
| 2035 | queue.push(function(cb) { |
||
| 2036 | fileFilters[name].call(self, rule, file, function(res) { |
||
| 2037 | cb(!res); |
||
| 2038 | }); |
||
| 2039 | }); |
||
| 2040 | } |
||
| 2041 | }); |
||
| 2042 | plupload.inSeries(queue, cb); |
||
| 2043 | } |
||
| 2044 | |||
| 2045 | /** |
||
| 2046 | * @method resolveFile |
||
| 2047 | * @private |
||
| 2048 | * @param {moxie.file.File|moxie.file.Blob|plupload.File|File|Blob|input[type="file"]} file |
||
| 2049 | */ |
||
| 2050 | function resolveFile(file) { |
||
| 2051 | var type = plupload.typeOf(file); |
||
| 2052 | |||
| 2053 | // moxie.file.File |
||
| 2054 | if (file instanceof o.file.File) { |
||
| 2055 | if (!file.ruid && !file.isDetached()) { |
||
| 2056 | if (!ruid) { // weird case |
||
| 2057 | return false; |
||
| 2058 | } |
||
| 2059 | file.ruid = ruid; |
||
| 2060 | file.connectRuntime(ruid); |
||
| 2061 | } |
||
| 2062 | resolveFile(new plupload.File(file)); |
||
| 2063 | } |
||
| 2064 | // moxie.file.Blob |
||
| 2065 | else if (file instanceof o.file.Blob) { |
||
| 2066 | resolveFile(file.getSource()); |
||
| 2067 | file.destroy(); |
||
| 2068 | } |
||
| 2069 | // plupload.File - final step for other branches |
||
| 2070 | else if (file instanceof plupload.File) { |
||
| 2071 | if (fileName) { |
||
| 2072 | file.name = fileName; |
||
| 2073 | } |
||
| 2074 | |||
| 2075 | queue.push(function(cb) { |
||
| 2076 | // run through the internal and user-defined filters, if any |
||
| 2077 | filterFile(file, function(err) { |
||
| 2078 | if (!err) { |
||
| 2079 | // make files available for the filters by updating the main queue directly |
||
| 2080 | files.push(file); |
||
| 2081 | // collect the files that will be passed to FilesAdded event |
||
| 2082 | filesAdded.push(file); |
||
| 2083 | |||
| 2084 | self.trigger("FileFiltered", file); |
||
| 2085 | } |
||
| 2086 | delay(cb, 1); // do not build up recursions or eventually we might hit the limits |
||
| 2087 | }); |
||
| 2088 | }); |
||
| 2089 | } |
||
| 2090 | // native File or blob |
||
| 2091 | else if (plupload.inArray(type, ['file', 'blob']) !== -1) { |
||
| 2092 | resolveFile(new o.file.File(null, file)); |
||
| 2093 | } |
||
| 2094 | // input[type="file"] |
||
| 2095 | else if (type === 'node' && plupload.typeOf(file.files) === 'filelist') { |
||
| 2096 | // if we are dealing with input[type="file"] |
||
| 2097 | plupload.each(file.files, resolveFile); |
||
| 2098 | } |
||
| 2099 | // mixed array of any supported types (see above) |
||
| 2100 | else if (type === 'array') { |
||
| 2101 | fileName = null; // should never happen, but unset anyway to avoid funny situations |
||
| 2102 | plupload.each(file, resolveFile); |
||
| 2103 | } |
||
| 2104 | } |
||
| 2105 | |||
| 2106 | ruid = getRUID(); |
||
| 2107 | |||
| 2108 | resolveFile(file); |
||
| 2109 | |||
| 2110 | if (queue.length) { |
||
| 2111 | plupload.inSeries(queue, function() { |
||
| 2112 | // if any files left after filtration, trigger FilesAdded |
||
| 2113 | if (filesAdded.length) { |
||
| 2114 | self.trigger("FilesAdded", filesAdded); |
||
| 2115 | } |
||
| 2116 | }); |
||
| 2117 | } |
||
| 2118 | }, |
||
| 2119 | |||
| 2120 | /** |
||
| 2121 | * Removes a specific file. |
||
| 2122 | * |
||
| 2123 | * @method removeFile |
||
| 2124 | * @param {plupload.File|String} file File to remove from queue. |
||
| 2125 | */ |
||
| 2126 | removeFile : function(file) { |
||
| 2127 | var id = typeof(file) === 'string' ? file : file.id; |
||
| 2128 | |||
| 2129 | for (var i = files.length - 1; i >= 0; i--) { |
||
| 2130 | if (files[i].id === id) { |
||
| 2131 | return this.splice(i, 1)[0]; |
||
| 2132 | } |
||
| 2133 | } |
||
| 2134 | }, |
||
| 2135 | |||
| 2136 | /** |
||
| 2137 | * Removes part of the queue and returns the files removed. This will also trigger the |
||
| 2138 | * FilesRemoved and QueueChanged events. |
||
| 2139 | * |
||
| 2140 | * @method splice |
||
| 2141 | * @param {Number} [start=0] Start index to remove from. |
||
| 2142 | * @param {Number} [length] Number of files to remove (defaults to number of files in the queue). |
||
| 2143 | * @return {Array} Array of files that was removed. |
||
| 2144 | */ |
||
| 2145 | splice : function(start, length) { |
||
| 2146 | // Splice and trigger events |
||
| 2147 | var removed = files.splice(start === undef ? 0 : start, length === undef ? files.length : length); |
||
| 2148 | |||
| 2149 | // if upload is in progress we need to stop it and restart after files are removed |
||
| 2150 | var restartRequired = false; |
||
| 2151 | if (this.state == plupload.STARTED) { // upload in progress |
||
| 2152 | plupload.each(removed, function(file) { |
||
| 2153 | if (file.status === plupload.UPLOADING) { |
||
| 2154 | restartRequired = true; // do not restart, unless file that is being removed is uploading |
||
| 2155 | return false; |
||
| 2156 | } |
||
| 2157 | }); |
||
| 2158 | |||
| 2159 | if (restartRequired) { |
||
| 2160 | this.stop(); |
||
| 2161 | } |
||
| 2162 | } |
||
| 2163 | |||
| 2164 | this.trigger("FilesRemoved", removed); |
||
| 2165 | |||
| 2166 | // Dispose any resources allocated by those files |
||
| 2167 | plupload.each(removed, function(file) { |
||
| 2168 | file.destroy(); |
||
| 2169 | }); |
||
| 2170 | |||
| 2171 | if (restartRequired) { |
||
| 2172 | this.start(); |
||
| 2173 | } |
||
| 2174 | |||
| 2175 | return removed; |
||
| 2176 | }, |
||
| 2177 | |||
| 2178 | /** |
||
| 2179 | Dispatches the specified event name and its arguments to all listeners. |
||
| 2180 | |||
| 2181 | @method trigger |
||
| 2182 | @param {String} name Event name to fire. |
||
| 2183 | @param {Object..} Multiple arguments to pass along to the listener functions. |
||
| 2184 | */ |
||
| 2185 | |||
| 2186 | // override the parent method to match Plupload-like event logic |
||
| 2187 | dispatchEvent: function(type) { |
||
| 2188 | var list, args, result; |
||
| 2189 | |||
| 2190 | type = type.toLowerCase(); |
||
| 2191 | |||
| 2192 | list = this.hasEventListener(type); |
||
| 2193 | |||
| 2194 | if (list) { |
||
| 2195 | // sort event list by priority |
||
| 2196 | list.sort(function(a, b) { return b.priority - a.priority; }); |
||
| 2197 | |||
| 2198 | // first argument should be current plupload.Uploader instance |
||
| 2199 | args = [].slice.call(arguments); |
||
| 2200 | args.shift(); |
||
| 2201 | args.unshift(this); |
||
| 2202 | |||
| 2203 | for (var i = 0; i < list.length; i++) { |
||
| 2204 | // Fire event, break chain if false is returned |
||
| 2205 | if (list[i].fn.apply(list[i].scope, args) === false) { |
||
| 2206 | return false; |
||
| 2207 | } |
||
| 2208 | } |
||
| 2209 | } |
||
| 2210 | return true; |
||
| 2211 | }, |
||
| 2212 | |||
| 2213 | /** |
||
| 2214 | Check whether uploader has any listeners to the specified event. |
||
| 2215 | |||
| 2216 | @method hasEventListener |
||
| 2217 | @param {String} name Event name to check for. |
||
| 2218 | */ |
||
| 2219 | |||
| 2220 | |||
| 2221 | /** |
||
| 2222 | Adds an event listener by name. |
||
| 2223 | |||
| 2224 | @method bind |
||
| 2225 | @param {String} name Event name to listen for. |
||
| 2226 | @param {function} fn Function to call ones the event gets fired. |
||
| 2227 | @param {Object} [scope] Optional scope to execute the specified function in. |
||
| 2228 | @param {Number} [priority=0] Priority of the event handler - handlers with higher priorities will be called first |
||
| 2229 | */ |
||
| 2230 | bind: function(name, fn, scope, priority) { |
||
| 2231 | // adapt moxie EventTarget style to Plupload-like |
||
| 2232 | plupload.Uploader.prototype.bind.call(this, name, fn, priority, scope); |
||
| 2233 | }, |
||
| 2234 | |||
| 2235 | /** |
||
| 2236 | Removes the specified event listener. |
||
| 2237 | |||
| 2238 | @method unbind |
||
| 2239 | @param {String} name Name of event to remove. |
||
| 2240 | @param {function} fn Function to remove from listener. |
||
| 2241 | */ |
||
| 2242 | |||
| 2243 | /** |
||
| 2244 | Removes all event listeners. |
||
| 2245 | |||
| 2246 | @method unbindAll |
||
| 2247 | */ |
||
| 2248 | |||
| 2249 | |||
| 2250 | /** |
||
| 2251 | * Destroys Plupload instance and cleans after itself. |
||
| 2252 | * |
||
| 2253 | * @method destroy |
||
| 2254 | */ |
||
| 2255 | destroy : function() { |
||
| 2256 | this.trigger('Destroy'); |
||
| 2257 | settings = total = null; // purge these exclusively |
||
| 2258 | this.unbindAll(); |
||
| 2259 | } |
||
| 2260 | }); |
||
| 2261 | }; |
||
| 2262 | |||
| 2497 | })); |